<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ajax extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->model('product/product_model');
	}

	public function index(){
		$data['product_newest'] = $this->product_model->get_product(0, 0, 6, 0, 'create_date', 'DESC');
		$data['product_hot'] = $this->product_model->get_product_hot(0, 6);
		$data['tpl'] = 'home/index';
		$this->load->view('_layout/index', $data);
	}

	function err_404(){
	    $data['tpl'] = 'home/err_404';
	    $this->load->view('_layout/index', $data);
	}

	function uploader(){
	    try {
	        $upload_type = html_escape($this->input->get('type'));
	        $upload_id   = $this->input->get('id');
	        $mode        = $this->input->get('mode');
	        $result      = array();

	        echo md5($upload_id);
	        die();

	        //Check upload data
	        if (empty($upload_type) || empty($upload_id)){
	            die(json_encode(array('status' => false, 'msg' => 'param is missing')));
	        }

	        //Load library for upload, image
	        include_once APPPATH.'libraries/fileuploader.php';
	        $this->load->library('image_lib');

	        //Create & get path uploader dir
	        $upload_path = dirname($_SERVER['SCRIPT_FILENAME']).'/data/images/'.$upload_type.'/'.$upload_id.'/';
	        while (!is_dir($upload_path)) {
	            @mkdir($upload_path, 0755, true);
	        }

	        // list of valid extensions, ex. array("jpeg", "xml", "bmp")
	        $allowedExtensions = array('jpeg', 'jpg', 'png');

	        // max file size in bytes
	        $sizeLimit = 2 * 1024 * 1024;

	        //Create a instance uploader
	        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);

	        //Upload file
	        $result = $uploader->handleUpload($upload_path);

	        //File uploaded name
	        $file_uploaded = $upload_path.$result['filename'];

	        if ($mode == 'simple'){
	            $max_width  = 500;
	            $max_height = 500;
	            $filename = trim($this->input->get('filename'));

	            //Rename file after upload
	            $filename_new = $filename != '' ? $filename : md5($upload_id).'.jpg';
	            @rename($upload_path.$result['filename'], $upload_path.$filename_new);

	            //Resize
	            setMemoryForImage($upload_path.$filename_new);
	            $config = array(
	                    'source_image'       => $upload_path.$filename_new,
	                    'maintain_ratio'     => true
	            );
	            $dataimage               = getimagesize($upload_path.$filename_new);
	            if (($dataimage[0] > $max_width) || ($dataimage[1] > $max_height)) {
	                $config['width']     = $max_width;
	                $config['height']    = $max_height;
	            }
	            $this->image_lib->initialize($config);
	            $this->image_lib->resize();

	            $result['type']     = $upload_type;
	            $result['id']       = $upload_id;
	            $result['mode']     = $mode;
	            $result['id']       = $upload_id;
	            $result['type']     = $upload_type;
	            $result['image']    = $filename_new;
	            $result['url']      = 'uploads/'.$upload_type.'/'.$upload_id.'/'.$filename_new.'?t='.time();
	            $result['path']     = $upload_path;
	        } else {
	            $upload_id   = !empty($upload_id) ? $upload_id : random_string('alpha',6);

	            $max_width  = 1024;
	            $max_height = 1024;

	            //Rename file after upload
	            $filename_new = random_string('unique').'.'.strtolower($result['ext']);
	            @fclose($upload_path.$result['filename']);
	            if (file_exists($upload_path.$filename_new)) {
	                $filename_new = random_string('unique').'.'.strtolower($result['ext']);
	            }
	            @rename($upload_path.$result['filename'], $upload_path.$filename_new);

	            //Resize
	            setMemoryForImage($upload_path.$filename_new);
	            $config = array(
	                    'source_image'       => $upload_path.$filename_new,
	                    'maintain_ratio'     => true
	            );
	            $dataimage               = getimagesize($upload_path.$filename_new);
	            if (($dataimage[0] > $max_width) || ($dataimage[1] > $max_height)) {
	                $config['width']     = $max_width;
	                $config['height']    = $max_height;
	            }
	            $this->image_lib->initialize($config);
	            $this->image_lib->resize();

	            //Update database
	            $gallery = array(
	                    'type'               => $upload_type,
	                    'item_id'            => $upload_id,
	                    'image'              => $filename_new,
	                    'active_flag'        => ACTIVE_FLAG,
	                    'update_date'        => date('Y-m-d H:i:s', time()),
	                    'create_date'        => date('Y-m-d H:i:s', time()),
	            );
	            $this->db->insert(TBL_GALLERY, $gallery);

	            //Update info response for client
	            $result['gallery_id']    = $this->db->insert_id();
	            $result['id']            = $upload_id;
	            $result['type']          = $upload_type;
	            $result['image']         = $filename_new;
	            $result['url']           = 'images/w100-h80-c-100.80/'.SLIR_PREFIX.'data/images/'.$upload_type.'/'.$upload_id.'/'.$filename_new;
	            $result['path']          = $upload_path;
	            $result['resize']        = $dataimage;
	            #$result['file_upload']  = $file_uploaded;
	        }
	        echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
	    } catch (Exception $e){
	        _debug($e);
	    }
	}
}